<?php

class Flats_modul  extends CI_Model {
	public $select;
	public $dollars;
 
	public function __construct() {
		// Call the Model constructor
		parent::__construct();

		$this -> fproperty();
		$this-> dollars=30;
	}

 

    /**
     * Поиск наглавной главная
     *
     * @return void
     * @author
     */

    public function request($get = false, $start = 0, $finish = 30) {

	 
        $data = false;
        $select_data = array();
        $DB2 = $this -> load -> database('adsent', TRUE, TRUE);
 
       $select = "SELECT    
    r.id,
    r.kitchen,
    r.area,
    r.l_area,
    r.t_area,
    r.adress_name,
    r.room,
    r.region_names,
    r.region_id,
    r.city_name, 
    r.city_id,
    
    
    r.object_type,
    r.object_type2,
    r.ipoteka,
    r.sost_type,
    r.points,
    r.lift,
    r.g_lift,
    r.windows_type,
    r.balkon,
    r.loggia,
    r.su_r,
    r.su_s,
      
    r.newtime,
    
    
 
    
    
    
    r.street_name,
    r.street_id,
    r.home_id,
    r.id_metro,
    r.min_user,
    r.goby,
    r.price,
    r.currency,  
    r.floor,
    r.floorn,  
    r.phone, 
    r.balkon,
    r.comment_client,
    r.comment_site,
    r.keepday,
    r.for_all,
    r.contact_telefon1,
    r.contact_name1,
    r.contact_telefon2,
    r.contact_name2,
    r.id_phone,
    r.act,
    r.datetime,
    r.moderator,
    
    r.name_metro,
    r.id_metro
  
   FROM  `sflat` `r`   
   $get 
   GROUP BY r.id
   
   
   LIMIT $start,$finish;";

        $q = $DB2 -> query($select);
        $data = $this->req($q);
        return $data;
    }

 
 
  
  
	public function fproperty() {
		$lt = false;

		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);
		$query = $DB2 -> query("SELECT * FROM `property`");

		if ($query -> num_rows() > 0) {
			foreach ($query->result() as $row) {
				$lt[$row -> text] = $row -> val; 
			}
		}
		$this->property = 	$lt;
		return;
	}
	
 
	
   
    
    
    /**
     * 
     * @param $array - массив с данными для разбора 
     * 
     * */
    public function req($datas){

	$this -> load -> helper('text');
   	$this -> load -> helper('see');
     $data =  array();
        if ($datas -> num_rows() > 0) {

            foreach ($datas -> result() as $row) {

                $data[] = array(
  
                
                'id'  => $row -> id,
                'room'  => flat_type($row -> room), 
                'r_kitchen'  => $row -> kitchen,
                'r_area'  => $row -> area,
                'l_area'  => $row -> l_area,
                't_area'  => $row -> t_area,
                'r_adress_name'  => $row -> adress_name,
                'r_region_names'  => $row -> region_names,
                'r_region_id'  => $row -> region_id,
                'r_city_name'  => $row -> city_name,
                'r_city_id'  => $row -> city_id,
                'r_street_name'  => $row -> street_name,
                'r_street_id'  => $row -> street_id,
                'r_home_id'  => $row -> home_id,
                'r_id_metro'  => $row -> id_metro,
                'min_user'  => $row -> min_user,
                
                
                
                
                
                
                
                'object_type'  => $row -> object_type,
                'object_type2'  => $row -> object_type2,
                'sost_type'  => $row -> sost_type,
                'ipoteka'  => $row -> ipoteka,
                'points'  => $row -> points,
                'windows_type'  => $row -> windows_type,
                'balkon'  => $row -> balkon,
                'loggia'  => $row -> loggia,
                'su_r'  => $row -> su_r,
                'su_s'  => $row -> su_s,
                
                    
                 
                // пешком до метро
                'goby'  =>  rent_flat_metro_distance($row -> goby),
                
                'newtime'  => $row -> newtime,
                
                
                
                
             'price_dollar' => rent_flat_current($row -> price, $row -> currency,'dollars',$this->dollars),
				   'price_rubley' => rent_flat_current($row -> price, $row -> currency, 'rubley',$this->dollars),
				
                'currency'  => $row -> currency,
                
                'floorn'  => $row -> floorn,
                
                'floor'  => $row -> floor,
                
                
                
                'phone'  => $row -> phone,
                'balkon'  => $row -> balkon,
                
                 
                'comment_client'  => $row -> comment_client,
                'r_comment_site'  => $row -> comment_site,
                'r_keepday'  => $row -> keepday,
                'for_day'  => sale_suburbian_for_days($row -> for_all),
                'contact_telefon1'  => $row -> contact_telefon1,
                'contact_name1'  => $row -> contact_name1,
                'contact_telefon2'  => $row -> contact_telefon2,
                'contact_name2'  => $row -> contact_name2,
                'r_id_phone'  => $row -> id_phone,
                'r_act'  => $row -> act,
                'datetime'  => $row -> datetime,
                'r_moderator'  => $row -> moderator,
                
                
                
                'metro' => $row -> name_metro, 
                'metro_id' => $row -> id_metro, 
                'adsent' => 'sflat', 
                 );
            }
        }

        return $data;
    }
    
   
}
